package com.founder.android.parser;

import org.xml.sax.Attributes;

import com.founder.android.data.WeatherCondition;
import com.founder.android.data.WeatherInfo;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;

/**
 * 
 * @author chen_weihua
 * 
 * <?xml version="1.0"?>
 * <xml_api_reply version="1">
 *   <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >
 *     <forecast_information>
 *       <city data="Nantong, Jiangsu"/>
 *       <postal_code data="南通"/>
 *       <latitude_e6 data=""/>
 *       <longitude_e6 data=""/>
 *       <forecast_date data="2011-12-02"/>
 *       <current_date_time data="2011-12-02 20:30:00 +0000"/>
 *       <unit_system data="SI"/>
 *     </forecast_information>
 *     <current_conditions>
 *       <condition data="多云"/>
 *       <temp_f data="46"/>
 *       <temp_c data="8"/>
 *       <humidity data="湿度： 50%"/>
 *       <icon data="/ig/images/weather/cn_cloudy.gif"/>
 *       <wind_condition data="风向： 西北、风速：7 米/秒"/>
 *     </current_conditions>
 *     <forecast_conditions>
 *       <day_of_week data="周五"/>
 *       <low data="2"/>
 *       <high data="8"/>
 *       <icon data="/ig/images/weather/mostly_sunny.gif"/>
 *       <condition data="以晴为主"/>
 *     </forecast_conditions>
 *     <forecast_conditions>
 *       <day_of_week data="周六"/>
 *       <low data="5"/>
 *       <high data="9"/>
 *       <icon data="/ig/images/weather/sunny.gif"/>
 *       <condition data="晴"/>
 *      </forecast_conditions>
 *      <forecast_conditions>
 *        <day_of_week data="周日"/>
 *        <low data="9"/>
 *        <high data="12"/>
 *        <icon data="/ig/images/weather/mostly_sunny.gif"/>
 *        <condition data="晴间多云"/>
 *      </forecast_conditions>
 *      <forecast_conditions>
 *        <day_of_week data="周一"/>
 *        <low data="12"/>
 *        <high data="12"/>
 *        <icon data="/ig/images/weather/mostly_sunny.gif"/>
 *        <condition data="晴间多云"/>
 *      </forecast_conditions>
 *    </weather>
 *  </xml_api_reply>
 * 
 *
 */
public class WeatherXMLParserHandler implements XmlParserHandler {
	
	public static String WEATHER_HOST = "http://www.google.com.hk";
	
	public static String RESPONSE_CHARSET = "gb2312";
	
	private static final String ITEM = "weather";
	
	private static final String FORECAST_INFOMATION = "forecast_information";
	
	private static final String CITY = "city";
	
	private static final String CODE = "postal_code";
	
	private static final String CURRENT_DATE_TIME = "current_date_time";
	
	private static final String CURRENT_CONDITION = "current_conditions";
	
	private static final String CONDITION = "condition";
	
	private static final String ICON = "icon";
	
	private static final String TEMP_C = "temp_c";
	
	private static final String HUMIDITY = "humidity";
	
	private static final String WIND = "wind_condition";
	
	private static final String FORECAST_CONDITIONS = "forecast_conditions";
	
	private static final String DAY_OF_WEEK = "day_of_week";
	
	private static final String LOW = "low";
	
	private static final String HIGH = "high";
	
	private static final String PROBLEM_CAUSE = "problem_cause";
	
	private static final String DATA = "data";
	
	
	public static String getRequestURL(String city) {
		return WEATHER_HOST + "/ig/api?hl=zh-cn&weather=" + city;
	}
	
	public static String getImageURL(String url) {
		return WEATHER_HOST + url;
	}
	
	@Override
	public Copyable getPraseData() {
		return new WeatherInfo();
	}
	
	@Override
	public RootElement getRootElement() {
		return new RootElement("xml_api_reply");
	}
	
	@Override
	public Element getItem(RootElement root) {
		Element weather = root.getChild(ITEM);
		return weather;
	}
	
	@Override
	public void config(Element item, Copyable data) {
		final WeatherInfo weatherInfo = (WeatherInfo)data;
		//
		Element problem = item.getChild(PROBLEM_CAUSE);
		problem.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				weatherInfo.setSuccess(false);
				weatherInfo.setProblem(attributes.getValue(DATA));
			}
		});
		
		//config forecast_information
		Element info = item.getChild(FORECAST_INFOMATION);
		info.getChild(CITY).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				weatherInfo.setCity(attributes.getValue(DATA));
			}
		});
		info.getChild(CODE).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				weatherInfo.setPostalCode(attributes.getValue(DATA));
			}
		});
		info.getChild(CURRENT_DATE_TIME).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				weatherInfo.setCurrentDateTime(attributes.getValue(DATA));
			}
		});
		
		final WeatherCondition currentCondition = new WeatherCondition();
		Element currentElement = item.getChild(CURRENT_CONDITION);
		currentElement.setEndElementListener(new EndElementListener() {
			@Override
			public void end() {
				weatherInfo.setCurrentCondition((WeatherCondition)currentCondition.clone());
			}
		});
		currentElement.getChild(CONDITION).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				currentCondition.setCondition(attributes.getValue(DATA));
			}
		});
		currentElement.getChild(TEMP_C).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				currentCondition.setTempC(attributes.getValue(DATA));
			}
		});
		currentElement.getChild(HUMIDITY).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				currentCondition.setHumidity(attributes.getValue(DATA));
			}
		});
		currentElement.getChild(ICON).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				currentCondition.setIcon(attributes.getValue(DATA));
			}
		});
		currentElement.getChild(WIND).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				currentCondition.setWindCondition(attributes.getValue(DATA));
			}
		});
		
		final WeatherCondition forecastCondition = new WeatherCondition();
		Element forecastElement = item.getChild(FORECAST_CONDITIONS);
		forecastElement.setEndElementListener(new EndElementListener() {
			@Override
			public void end() {
				weatherInfo.addForecastCondition((WeatherCondition)forecastCondition.clone());
			}
		});
		forecastElement.getChild(DAY_OF_WEEK).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				forecastCondition.setDayOfWeek(attributes.getValue(DATA));
			}
		});
		forecastElement.getChild(ICON).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				forecastCondition.setIcon(attributes.getValue(DATA));
			}
		});
		forecastElement.getChild(CONDITION).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				forecastCondition.setCondition(attributes.getValue(DATA));
			}
		});
		forecastElement.getChild(LOW).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				forecastCondition.setLow(attributes.getValue(DATA));
			}
		});
		forecastElement.getChild(HIGH).setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				forecastCondition.setHigh(attributes.getValue(DATA));
			}
		});
	}

}
